package splicing

import (
	"bytes"
	"fmt"
	"time"
	"unsafe"
)

var letterBytes = [...]byte{65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
	97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122}

var numberBytes = [...]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57}

// ErgodicLetter2 这个是目前的最佳实践，组合字符串
func ErgodicLetter2() {
	kk := [5]byte{}
	letter5 := kk[:5]
	letter4 := kk[:4]
	letter3 := kk[:3]
	letter2 := kk[:2]
	letter1 := kk[:1]
	now := time.Now()

	for i := 0; i < len(letterBytes); i++ {
		letter5[0] = letterBytes[i]
		mm1 := *(*string)(unsafe.Pointer(&letter1))
		if mm1 == "z" {
			fmt.Println("z")
		}
		for j := 0; j < len(letterBytes); j++ {
			letter5[0] = letterBytes[i]
			letter5[1] = letterBytes[j]
			mm2 := *(*string)(unsafe.Pointer(&letter2))
			if mm2 == "zz" {
				fmt.Println("zz")
			}
			for k := 0; k < len(letterBytes); k++ {
				letter5[0] = letterBytes[i]
				letter5[1] = letterBytes[j]
				letter5[2] = letterBytes[k]
				mm3 := *(*string)(unsafe.Pointer(&letter3))
				if mm3 == "zzz" {
					fmt.Println("zzz")
				}
				for n := 0; n < len(letterBytes); n++ {
					letter5[0] = letterBytes[i]
					letter5[1] = letterBytes[j]
					letter5[2] = letterBytes[k]
					letter5[3] = letterBytes[n]
					mm4 := *(*string)(unsafe.Pointer(&letter4))
					if mm4 == "zzzz" {
						fmt.Println("zzzz")
					}
					for m := 0; m < len(letterBytes); m++ {
						letter5[0] = letterBytes[i]
						letter5[1] = letterBytes[j]
						letter5[2] = letterBytes[k]
						letter5[3] = letterBytes[n]
						letter5[4] = letterBytes[m]
						mm5 := *(*string)(unsafe.Pointer(&letter5))
						if mm5 == "zzzzz" {
							fmt.Println("zzzzz")
						}
					}
				}
			}
		}
	}
	fmt.Println(now.Sub(time.Now()))
}

// ErgodicLetter 性能次之
func ErgodicLetter() {
	var bt bytes.Buffer
	now := time.Now()
	for i := 0; i < len(letterBytes); i++ {
		bt.Reset()
		bt.WriteByte(letterBytes[i])
		// TODO a
		mm1 := bt.String()
		if mm1 == "z" {
			fmt.Println("z")
		}
		for j := 0; j < len(letterBytes); j++ {
			bt.Reset()
			bt.WriteByte(letterBytes[i])
			bt.WriteByte(letterBytes[j])
			// TODO ab
			mm2 := bt.String()
			if mm2 == "zz" {
				fmt.Println("zz")
			}
			for k := 0; k < len(letterBytes); k++ {
				bt.Reset()
				bt.WriteByte(letterBytes[i])
				bt.WriteByte(letterBytes[j])
				bt.WriteByte(letterBytes[k])
				// TODO abc
				mm3 := bt.String()
				if mm3 == "zzz" {
					fmt.Println("zzz")
				}
				for n := 0; n < len(letterBytes); n++ {
					bt.Reset()
					bt.WriteByte(letterBytes[i])
					bt.WriteByte(letterBytes[j])
					bt.WriteByte(letterBytes[k])
					bt.WriteByte(letterBytes[n])
					// TODO abcd
					mm4 := bt.String()
					if mm4 == "zzzz" {
						fmt.Println("zzzz")
					}
					for m := 0; m < len(letterBytes); m++ {
						bt.Reset()
						bt.WriteByte(letterBytes[i])
						bt.WriteByte(letterBytes[j])
						bt.WriteByte(letterBytes[k])
						bt.WriteByte(letterBytes[n])
						bt.WriteByte(letterBytes[m])
						// TODO abcdf
						mm5 := bt.String()
						if mm5 == "zzzzz" {
							fmt.Println("zzzzz")
						}
					}
				}
			}
		}

	}
	fmt.Println(now.Sub(time.Now()))
}

func ErgodicNumber() {
	var bt bytes.Buffer
	for i := 0; i < len(numberBytes); i++ {
		bt.Reset()
		bt.WriteByte(numberBytes[i])
		// TODO 1
		for j := 0; j < len(numberBytes); j++ {
			bt.Reset()
			bt.WriteByte(numberBytes[i])
			bt.WriteByte(numberBytes[j])
			// TODO 12
			for k := 0; k < len(numberBytes); k++ {
				bt.Reset()
				bt.WriteByte(numberBytes[i])
				bt.WriteByte(numberBytes[j])
				bt.WriteByte(numberBytes[k])
				// TODO 123
				for n := 0; n < len(numberBytes); n++ {
					bt.Reset()
					bt.WriteByte(numberBytes[i])
					bt.WriteByte(numberBytes[j])
					bt.WriteByte(numberBytes[k])
					bt.WriteByte(numberBytes[n])
					// TODO 1234
					for m := 0; m < len(numberBytes); m++ {
						bt.Reset()
						bt.WriteByte(numberBytes[i])
						bt.WriteByte(numberBytes[j])
						bt.WriteByte(numberBytes[k])
						bt.WriteByte(numberBytes[n])
						bt.WriteByte(numberBytes[m])
						// TODO 12345
					}
				}
			}
		}
	}
}
